﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ScheduleManagement.BLL
{
    /// <summary>
    /// 用户信息操作类
    /// </summary>
    public class User_BLL
    {
        //实例化数据访问层_用户信息操作类
        ScheduleManagement.DAL.User_Dal userSQLOperation = new ScheduleManagement.DAL.User_Dal();
        //实例化数据访问层_登陆处理类 验证用户名是否存在时要用到
        ScheduleManagement.DAL.Login_Dal loginDal = new ScheduleManagement.DAL.Login_Dal(); 
        //实例化用户信息实体
        //ScheduleManagement.MODEL.Users userinfo = new ScheduleManagement.MODEL.Users();

        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <param name="username">新用户名</param>
        /// <param name="psw">密码</param>
        /// <param name="pswcheck">确认密码</param>
        /// <returns></returns>
        public bool Adduser(string username, string psw, string pswcheck)
        {
            ScheduleManagement.MODEL.Users.U_Name = username;
            ScheduleManagement.MODEL.Users.U_Password = "";
            if (ScheduleManagement.MODEL.Users.U_Name.Equals(string.Empty))
            {
                throw new Exception("用户名不能为空，请重新输入！");
            }
            else
            {
                if (psw!=pswcheck)
                {
                    throw new Exception("两次密码不一致，请重新输入！");                                                
                }
                else if (loginDal.CheckUser() != 1)
                {
                    throw new Exception("用户名已经存在！");
                }
                else
                {
                    ScheduleManagement.MODEL.Users.U_Password = psw;
                    userSQLOperation.AddUser();
                    return true;
                }
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userinfo">Users_Model实体</param>
        /// <returns></returns>
        public void Deluser()
        {
            userSQLOperation.DelUser();
        }

        /// <summary>
        /// 检查用户是否合法并返回用户权限
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public int CheckUser()
        {
            if (ScheduleManagement.MODEL.Users.U_Name.Equals(string.Empty))
            {
                throw new Exception("用户名不能为空");
            }
            else
            {
                int flag = userSQLOperation.CheckUser();//检查用户是否合法并返回权限
                return flag;
            }
        }

        /// <summary>
        /// 验证用户管理密码是否正确
        /// </summary>
        /// <param name="admininfo"></param>
        /// <param name="adminpsw"></param>
        /// <returns></returns>
        public bool CheckAdmin()
        {
            string tempPsw = userSQLOperation.CheckAdmin();
            if (tempPsw.Equals(ScheduleManagement.MODEL.Admininfo.A_Password))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 权限转移
        /// </summary>
        /// <param name="usrename">转移对象</param>
        public void ChangeAdmin(string username)
        {
            userSQLOperation.ChangeAdmin(username);//设置该用户管理员权限
        }
    }
}
